package com.baosight.risk.common.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;

/**
 * JSON解析处理
 *
 * @author duanyongqiang
 */
public class JSONUtils {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    private static final ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter();

    public static void marshal(File file, Object value) throws Exception {
        objectWriter.writeValue(file, value);
    }

    public static void marshal(OutputStream os, Object value) throws Exception {
        objectWriter.writeValue(os, value);
    }

    public static String marshal(Object value) throws Exception {
        return objectWriter.writeValueAsString(value);
    }

    public static byte[] marshalBytes(Object value) throws Exception {
        return objectWriter.writeValueAsBytes(value);
    }

    public static <T> T unmarshal(File file, Class<T> valueType) throws Exception {
        return objectMapper.readValue(file, valueType);
    }

    public static <T> T unmarshal(InputStream is, Class<T> valueType) throws Exception {
        return objectMapper.readValue(is, valueType);
    }

    public static <T> T unmarshal(String str, Class<T> valueType) throws Exception {
        return objectMapper.readValue(str, valueType);
    }

    public static <T> T unmarshal(byte[] bytes, Class<T> valueType) throws Exception {
        if (bytes == null) {
            bytes = new byte[0];
        }
        return objectMapper.readValue(bytes, 0, bytes.length, valueType);
    }

    /**
     * 将JSONArray中包含的null和JSONNull属性修改成""
     *
     * @param jsonArray
     * @return
     */
    public static JSONArray filterJSONArrayNull(JSONArray jsonArray) {
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            filterJSONObjectNull(jsonObject);
        }
        return jsonArray;
    }

    /**
     * 将jsonObject中包含的null和JSONNull属性修改成""
     *
     * @param jsonObj
     * @return
     */
    public static JSONObject filterJSONObjectNull(JSONObject jsonObj) {
        Iterator<String> it = jsonObj.keys();
        Object obj = null;
        String key = null;
        while (it.hasNext()) {
            key = it.next();
            obj = jsonObj.get(key);
            if (obj instanceof JSONObject) {
                filterJSONObjectNull((JSONObject) obj);
            }
            if (obj instanceof JSONArray) {
                JSONArray objArr = (JSONArray) obj;
                for (int i = 0; i < objArr.size(); i++) {
                    filterJSONObjectNull(objArr.getJSONObject(i));
                }
            }
            if (obj instanceof JSONNull) {
                jsonObj.put(key, "");
            }
        }
        return jsonObj;
    }
}
